<?php 
/**
 * 三国夫人
 */
class Module_kingdomslady{
	/**
	 * 结构函数
	 */
	function __construct(){
	
	}
	/**
	 * 游戏首页
	 */
	function index(){
		
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		
		loadLib('User');
		$info = User::getBaseInfo($uin);
		/********************初始用户数据*************************/
		$data = App_kingdomslady::getAddKingdoms($uin , false); 
		$newbie = false;
		if(empty($data)) {
			
			$data = App_kingdomslady::getAddKingdoms($uin , true);
			$newbie = true;
			// 发feed
			loadLib('Feed');
			Feed::addContent($uin , '三国夫人游戏' , 'normal' , array(
				array('name' => '进入游戏' , 'url' => SITEURL . '/app.php?m=kingdomslady')
			));
		}
		
		$smarty->assign('info' , $info);
		$smarty->assign('data' , $data);
		$smarty->assign('newbie' , $newbie);
		return $smarty->fetch('app/kingdoms_lady/index.tpl.php');
	}
	/**
	 * 国师府初始页面
	 */
	function country_teacher_initial(){
		
		$smarty = loadSmarty();
		
		return $smarty->fetch('app/kingdoms_lady/country_teacher.tpl.php');
	}
	/**
	 * 国师府拜师操作
	 */
	function country_teacher(){
		$mark = isset($_POST['mark']) ? intval($_POST['mark']) : null;
		if(empty($mark)){
			return View::error('参数错误');
		}
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		$smarty = loadSmarty();
		/*****************************获取游戏用户信息*****************************/
		$info = App_kingdomslady::getAddKingdoms($uin , true); 
		/*****************************获取已练习的武功秘籍最高层*****************************/
		$data = App_kingdomslady::getOneCheatsLv($uin);
		
		$lv = $data['lv'];
		
		$smarty->assign('lv' , $lv);
		
		$smarty->assign('data' , $info);
		if($mark == 1 && $lv >=5 && $info['chef'] == 0){
			App_kingdomslady::getAlterTeacher($uin, 1);
		}
		if($mark == 2 && $lv >=20 && $info['chef'] == 1){
			App_kingdomslady::getAlterTeacher($uin, 2);
		}
		if($mark == 3 && $lv >=50 && $info['chef'] == 2){
			App_kingdomslady::getAlterTeacher($uin, 3);
		}
		if($mark == 3 && $lv >=100 && $info['chef'] == 3){
			App_kingdomslady::getAlterTeacher($uin, 4);
		}
		$smarty->assign('mark' , $mark);
		return $smarty->fetch('app/kingdoms_lady/country_operate.tpl.php');
	}
	/**
	 * 进入国师府内堂初始页面
	 */
	function country_teacher_cella(){
		$uin = sessionInfo('uin');
	
		$smarty = loadSmarty();
		/*****************************获取游戏用户信息*****************************/
		loadLib('App_kingdomslady');
		$data = App_kingdomslady::getAddKingdoms($uin , true);
		$smarty->assign('data' , $data);
		return $smarty->fetch('app/kingdoms_lady/cella.tpl.php');
	}
	
	/**
	 * 练习武功秘籍页面
	 */
	function dance_studio_initial(){
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
	
		/*****************************获取游戏用户信息*****************************/
		loadLib('App_kingdomslady');
		$data = App_kingdomslady::getAddKingdoms($uin , true);
		
		/********************************获取武功秘籍******************************/
		$studio = App_kingdomslady::getDanceStudio();
		
		/********************************获取已学习的武功秘籍******************************/
		$skill = App_kingdomslady::getAllSkill($uin);
		
		if(!empty($skill)){
			foreach ($skill as $g => $row){
				$listf[] = $row['dance_studio_id'];			
			}
		}
		/************************获取时间********************/
		$time = App_kingdomslady::getTime();
		/************************获取一个时间段的内容********************/
		$coun_time = App_kingdomslady::getCountTime($uin , 'dance_studio' , 0 , $time[0]);
		if (!empty($coun_time)){
			App_kingdomslady::getAlterCount($uin, 'dance_studio' , 2);
		}
		$count = App_kingdomslady::getOneCount($uin , 'dance_studio' , false , 2);
		$newbie = false;
		if(empty($count)) {
			$count = App_kingdomslady::getOneCount($uin , 'dance_studio' , true ,2);
			$newbie = true;
		}
		
		/****************************获取正在学习的武功秘籍***************************/
		$state = App_kingdomslady::getAllState($uin , 'dance_studio');
		
		/************************循环判断未解锁及已学会武功秘籍**************************/
		foreach ($studio as $j => $row){
					
			if(!empty($skill)){
				if(!in_array($row['id'],$listf)){
					$list[$j] = $row;
				}
			}else{
				$list[$j] = $row;
			}
		}
			//print_r($skill);
		if(!empty($state)){
			/*************************计算练功结束时间*****************************/
			foreach ($state as $k => $row){
				$state[$k]['end_time_array'] = App_kingdomslady::getEndDate($state[$k]['object_id'] , $uin , $row['end_time']);
			}
			if(!empty($list)){
				foreach ($list as $h => $row){
					foreach ($state as $k => $result){
						if($row['id'] == $result['object_id']){
							$list[$h]['state'] =  $result;
						}
					}
				}
			}
		}
		/***********************获取今天已学了几本**********************/
		$skill_count = App_kingdomslady::getSkillCount($uin , $time[0] , $time[1]);
		$is_count = App_kingdomslady::getOneCount($uin , 'dance_studio' , true ,2);
		if (!empty($skill_count)){
			$is_uin_count = $is_count['count'] - $skill_count['count'];
		}else{
			$is_uin_count = $is_count['count'];
		}
		$smarty->assign('is_uin_count',$is_uin_count);
		$smarty->assign('data' , $data);
		if(!empty($list)){
			$is_list_array = array_slice($list,0,2);
			$smarty->assign('studio' , $is_list_array);
		}
		
		return $smarty->fetch('app/kingdoms_lady/dance_studio.tpl.php');
	}
	/**
	 * 练习武功秘籍
	 */
	function dance_studio(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$uin = sessionInfo('uin');
		
		loadLib('App_kingdomslady');
		/********************查询武功秘籍***********************/
		$studio = App_kingdomslady::getOneDanceStudio($id);
		if (empty($studio)){
			return View::error('该秘籍不存在！');
		}
		
		/********************查询是否还执行其他状态*********************/
		$is_belle = App_kingdomslady::getAllState($uin , 'map');
		if(!empty($is_belle)){
			return View::error('一心只能一用,你正在练功、或切磋、或救人,不能同时又练功哦.');
		}
		$data = App_kingdomslady::getOneSkill($uin, $id);
		if(!empty($data)){
			return View::error('你已学会这种武功,无需在学！');
		}
		/********************获取当天时间*********************/
		$is_time = App_kingdomslady::getTime();
		
		$skill_count = App_kingdomslady::getSkillCount($uin , $is_time[0] , $is_time[1]);
		
		$onestate = App_kingdomslady::getOneState($uin , 'dance_studio' , $id);
		$allstate = App_kingdomslady::getAllState($uin , 'dance_studio');
		
		$count = count($allstate);
		$is_count = $skill_count['count'] + $count ;
		if(!empty($allstate)){
			return View::error('一次至多学习1种武功!');
		}
		$count = App_kingdomslady::getOneCount($uin , 'dance_studio' , true ,2);
		if($skill_count['count'] >= $count['count'] || $is_count >= $count['count']){
			return View::error('你今天已经练成了'.$count['count'].'本功夫秘籍,太累了,明天再练吧.');
		}
		if(!empty($onestate)){
			return View::error('正在学习中...');
		}
		//print_r($allstate);
		
		//计算结束时间
		$end_time = time()+ $studio['required_time'] * 60 - 1;
		$code = App_kingdomslady::getAddState($uin , $id , 'dance_studio' , $end_time);
	
		if($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=dance_studio_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 练功时间已完成,收功
	 */
	function end_dance_studio(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$object_id = isset($_POST['object_id']) ? intval($_POST['object_id']) : null;
		
		if(empty($id) || empty($id)){
			return View::error('参数错误！');
		}
		loadLib('App_kingdomslady');
		/********************查询武功秘籍***********************/
		$studio = App_kingdomslady::getOneDanceStudio($object_id);
		if (empty($studio)){
			return View::error('该秘籍不存在！');
		}
		$uin = sessionInfo('uin');
		
		/********************查询状态是否练此功***********************/
		$list = App_kingdomslady::getOneState($uin, 'dance_studio', $object_id);
		if (empty($list)){
			return View::error('你没有练此武功！');
		}
		$data = App_kingdomslady::getOneSkill($uin, $object_id);
		if(!empty($data)){
			return View::error('你已学会这种武功,无需在学！');
		}
		$data = App_kingdomslady::getAddKingdoms($uin , true);
		$ap = $data['ap'] + $studio['ap'];
		
		/****************往个人秘籍添加******************/
		$code = App_kingdomslady::getAddSkill($uin , $object_id , $list['end_time']);
		if($code == 0){
			App_kingdomslady::getAlterAP($uin , $ap);
			/**************删除学习秘籍状态**************/
			App_kingdomslady::delState($uin , $id);
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=dance_studio_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 已学会的武功秘籍
	 */
	function skill(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$smarty = loadSmarty();
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		/********************************获取已学习的武功秘籍******************************/
		$skill = App_kingdomslady::getSkill($uin , $page , $per_page);
		$count = App_kingdomslady::getSkillCount($uin , 0 , time());
		
		$smarty->assign('skill' , $skill);
		$smarty->assign('per_page' , $per_page);
		$smarty->assign('count' , $count['count']);
		return $smarty->fetch('app/kingdoms_lady/skill.tpl.php');
	}
	/**
	 * 未解锁的武功秘籍
	 */
	function clear(){
		
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		
		loadLib('App_kingdomslady');
		
		/********************************获取武功秘籍******************************/
		$studio = App_kingdomslady::getDanceStudio();
		
		/********************************获取已学习的武功秘籍******************************/
		$skill = App_kingdomslady::getAllSkill($uin);
		
		if(!empty($skill)){
			foreach ($skill as $g => $row){
				$listf[] = $row['dance_studio_id'];			
			}
		}
		
		/************************循环判断未解锁及已学会武功秘籍**************************/
		foreach ($studio as $j => $row){
					
			if(!empty($skill)){
				if(!in_array($row['id'],$listf)){
					$list[$j] = $row;
				}
			}else{
				$list[$j] = $row;
			}
		}
		$is_list_array = array_slice($list,2,10);
		$smarty->assign('clear' , $is_list_array);
		
		return $smarty->fetch('app/kingdoms_lady/clear.tpl.php');
	}
	/**
	 * 点击查询武功秘籍详细
	 */
	function detail_dance_studio(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$studio = App_kingdomslady::getOneDanceStudio($id);
		
		if(empty($studio)){
			return View::error('该本武功秘籍不存在!');
		}
		
		$smarty->assign('studio' , $studio);
		return $smarty->fetch('app/kingdoms_lady/detail_dance_studio.tpl.php');
	}
	/**
	 * 兵营初始页面
	 */
	function general_initial(){
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		/********************初始兵营数据*************************/
		$data = App_kingdomslady::getAddBarracks($uin , false); 
		$newbie = false;
		if(empty($data)) {
			$data = App_kingdomslady::getAddBarracks($uin , true);
			$newbie = true;
		}
		
		/**********************查询个人资料***********************/
		$info = App_kingdomslady::getAddKingdoms($uin , true);
		
		/************************查询兵营等级**********************/
		$barracks_guide = App_kingdomslady::getBarracksGuide( $data['lv'] );
		
		/************************查询拥有的武将********************/
		$list = App_kingdomslady::getAllPossess($uin, 'general');
		$num = 0;
		if(!empty($list)){
			foreach ($list as $g => $row){
				$is_list[] = $row['object_id'];
				if($row['state'] == 1){
					$num++;
				}			
			}
		}
		$smarty->assign('count' , $num);
		/************************查询武将列表************************/
		$general = App_kingdomslady::getAllGeneral();
		foreach ($general as $j => $row){
			if(!empty($list)){
				foreach ($is_list as $k => $result){
					if($result == $row['id']){
						$is_list_array[$k] = $row;
					}
				}
			}
		}
		
		/****************************获取正在扩建的兵营***************************/
		$state = App_kingdomslady::getAllState($uin , 'barracks');
		if (!empty($state)){
			$state[0]['end_time_array'] = App_kingdomslady::getEndDate($state[0]['object_id'] , $uin , $state[0]['end_time']);
			$smarty->assign('state' , $state);
		}
		
		if(!empty($is_list_array)){
			foreach ($is_list_array as $k => $row){
				foreach ($list as $f => $result){
					if($row['id'] == $result['object_id']){
						$is_list_array[$f]['state'] =  $result;
					}
				}
			}
		}
		
		$smarty->assign('info' , $info);
		$smarty->assign('data' , $data);
		$smarty->assign('barracks_guide',$barracks_guide);
		if(!empty($is_list_array)){
			$smarty->assign('list' , $is_list_array);
		}
		return $smarty->fetch('app/kingdoms_lady/general.tpl.php');
	}
	/**
	 * 兵营开始扩建
	 */
	function barracks(){
		$upgrade = isset($_POST['upgrade']) ? intval($_POST['upgrade']) : null;
		if (empty($upgrade)){
			return View::error('参数错误');
		}
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		/*****************************获取已练习的武功秘籍最高层*****************************/
		$data = App_kingdomslady::getOneCheatsLv($uin);
		
		$lv = $data['lv'];
		
		if($lv < $upgrade){
			return View::error('你的等级还达不到扩建要求,扩建兵营需要练习武功秘籍'.$upgrade.'级');
		}
		
		//计算结束时间
		$end_time = time()+ 24 * 3600 - 1;
		/***********************把操作写进状态表*************************/
		$code = App_kingdomslady::getAddState($uin , $uin , 'barracks' , $end_time);
	
		if($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=general_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 兵营完成扩建
	 */
	function end_barracks(){
		$barracks_lv = isset($_POST['barracks_lv']) ? intval($_POST['barracks_lv']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if (empty($id)){
			return View::error('参数错误');
		}
		loadLib('App_Kingdomslady');
		$uin = sessionInfo('uin');
		$lv = $barracks_lv + 1;
		$code = App_kingdomslady::getAlterBarracks($uin, $lv);
		if ($code == 0){
			App_kingdomslady::delState($uin , $id);
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=general_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 大将随征
	 */
	function expedition(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if (empty($id)){
			return View::error('参数错误');
		}
		
		loadLib('App_Kingdomslady');
		$uin = sessionInfo('uin');
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (!empty($data)){
			return View::error('你正在闯关,无法带领大将...');
		}
		
		/**********************查询个人资料***********************/
		$data = App_kingdomslady::getAddKingdoms($uin , true);
		$possess = App_kingdomslady::getAllPossess($uin, 'general');
		
		$num = 0;
		foreach ($possess as $k => $row){
			if ($row['state'] == 1){
				$num++;
			}
		}
		$list = App_kingdomslady::getOnePossess($uin , $id);
		//print_r($list);
		if (!empty($list)){
			if ($list['state'] == 1){
				return View::error('此大将已在带领列表中!');
			}
		}
		
		if($data['chef'] <= $num){
			return View::error('你现在只能带' . $data['chef'] . '人随征,想带更多人请去国师府拜山头.');
		}
		/***********************把操作修改物品表*************************/
		$code = App_kingdomslady::getAlterPossess($uin , $id , 1 ,$list['is_ap']);
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=general_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 召回大将
	 */
	function recall(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if (empty($id)){
			return View::error('参数错误!!!');
		}
		
		$uin = sessionInfo('uin');
		loadLib('App_Kingdomslady');
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (!empty($data)){
			return View::error('该武将正在征战中!!!');
		}
		$list = App_kingdomslady::getOnePossess($uin , $id);
		if (empty($list)){
			return View::error('大将不存在!!!');
		}
		$code = App_kingdomslady::getAlterPossess($uin , $id , 0 ,$list['is_ap']);
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=general_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 丢弃武将
	 */
	function discard(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if (empty($id)){
			return View::error('参数错误!!!');
		}
		
		$uin = sessionInfo('uin');
		loadLib('App_Kingdomslady');
		
		$list = App_kingdomslady::getOnePossess($uin , $id);
		if (empty($list)){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=general_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}
		
		if ($list['state'] == 1){
			return View::error('武将随征中!!!');
		}
		
		$code = App_kingdomslady::delPossess($uin , $id);
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=general_initial&uin=' . sessionInfo('uin') . '&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 大将详细资料
	 */
	function detail_general(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneGeneral($id);
		
		if(empty($list)){
			return View::error('该武将资料不存在!');
		}
		
		$smarty->assign('list' , $list);
		return $smarty->fetch('app/kingdoms_lady/detail_general.tpl.php');
	}
	/**
	 * 铜雀台初始化
	 */
	function cs_platform_initial(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$smarty = loadSmarty();
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		/************************获取时间********************/
		$time = App_kingdomslady::getTime();
		/************************获取一个时间段的内容********************/
		$possess_time = App_kingdomslady::getPossessTime($uin , 'belle' , 0 , $time[0]);
		
		if (!empty($possess_time)){
			foreach ($possess_time as $k => $row){
				App_kingdomslady::getAlterPossess($uin, $row['id'], 0 , $row['is_ap']);
			}
		}
		//print_r($possess_time);
		/************************查询拥有的美女********************/
		$list = App_kingdomslady::getAllPossess($uin, 'belle');
		
		if(!empty($list)){
			foreach ($list as $k => $row){
				$is_list[] = $row['object_id'];
				
			}
			$count = count($is_list);
			$smarty->assign('count' , $count);
		}
		
		/************************查询美女表***********************/
		$belle = App_kingdomslady::getAllBelle();
		
		foreach ($belle as $j => $row){
			if(!empty($list)){
				if(in_array($row['id'],$is_list)){
					$is_array[] = $row;
				}
			}
		}
		
		if(!empty($is_array)){
			foreach ($list as $k => $row){
				$is_time = App_kingdomslady::getDate($row['time']);
				foreach ($is_array as $j => $rs){
					if ($row['object_id'] == $rs['id']){
						$list[$k]['rs'] = $rs;
						
					}
				}
				$list[$k]['time'] = $is_time;
			}
			$page = ($page-1) * $per_page;
			$is_list_array = array_slice($list,$page,$per_page);
			$smarty->assign('list' , $is_list_array);
		}
		
		$smarty->assign('per_page' , $per_page);
		
		return $smarty->fetch('app/kingdoms_lady/cs_platform.tpl.php');
	}
	/**
	 * 三国美女详细资料
	 */
	function cs_platform(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		if (empty($id)){
			return View::error('参数错误！');
		}
		$list = App_kingdomslady::getOneBelle($id);
		
		if(empty($list)){
			return View::error('该三国美女资料不存在!');
		}
		
		$smarty->assign('list' , $list);
		return $smarty->fetch('app/kingdoms_lady/detail_cs_platform.tpl.php');
	}
	/**
	 * 三国美女查看相片
	 */
	function cs_platform_photo(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneBelle($id);
		
		if(empty($list)){
			return View::error('该三国美女资料不存在!');
		}
		
		$smarty->assign('list' , $list);
		return $smarty->fetch('app/kingdoms_lady/cs_platform_photo.tpl.php');
	}
	/**
	 * 逗乐操作
	 */
	function cs_platform_tickle(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		if(empty($id)){
			return View::error('参数错误!');
		}
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		/**********************查询三国美女状态**********************/
		$list = App_kingdomslady::getOnePossess($uin , $id);
		
		if(empty($list)){
			return View::error('该三国美女资料不存在!');
		}
		if($list['state'] == 1){
			return View::error('该三国美女你今天已逗乐!');
		}
		/**********************查询个人解救三国美女**********************/
		$belle = App_kingdomslady::getAllPossess($uin, 'belle');
		$num = 0 ;
		if (!empty($belle)){
			foreach ($belle as $k => $row){
				if ($row['state'] == 1){
					$num++;
				}
			}
		}
		if ($num >= 3){
			return View::error('一天累计只能逗乐3次哦!');
		}
		
		/***********************把操作修改物品表*************************/
		$code = App_kingdomslady::getAlterPossess($uin , $id , 1 , $list['is_ap']);
		
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=tickle&id='. $id .'&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 逗乐页面
	 */
	function tickle(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getOneBelle($id);
		
		if(empty($list)){
			return View::error('该三国美女资料不存在!');
		}
		
		$joke_list = App_kingdomslady::getOneRandJoke();
		
		$smarty->assign('joke' , $joke_list['name']);
		$smarty->assign('list' , $list);
		return $smarty->fetch('app/kingdoms_lady/cs_platform_tickle.tpl.php');
	}
	/**
	 * 状元殿
	 */
	function number_one_scholar(){
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		/***********************获取武功秘籍最高等级人物信息*************************/
		$cheats_lv = App_kingdomslady::getAllCheatsLv();
		/***********************获取解救美女数量*************************/
		$possess_count = App_kingdomslady::getAllCount('belle');
		loadLib('User');
		$uin = sessionInfo('uin');
		/*************看是否有记录***************/
		$count = App_kingdomslady::getOneCount($uin , 'belle' ,false,0);
		
		$newbie = false;
		if(empty($count)) {
			$count = App_kingdomslady::getOneCount($uin , 'belle' ,true);
			$newbie = true;
		}
		if (!empty($cheats_lv)){
			$cheats_lv[0]['info'] = User::getBaseInfo($cheats_lv[0]['uin']);
			$smarty->assign('cheats_lv' , $cheats_lv[0]);
		}
		if (!empty($possess_count)){
			$possess_count[0]['info'] = User::getBaseInfo($possess_count[0]['uin']);
			$smarty->assign('possess_count' , $possess_count[0]);
		}
		
		return $smarty->fetch('app/kingdoms_lady/number_one_scholar.tpl.php');
	}
	/**
	 * 排行榜
	 */
	function top(){
		$type = isset($_GET['type']) ? trim($_GET['type']) : null;
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$smarty = loadSmarty();
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		/******************救美最多榜********************/
		if ($type == 'moko_top_girl'){
			$list = App_kingdomslady::getAllCount('belle');
		}
		/******************等级最高榜********************/
		if ($type == 'highest_lv'){
			$list = App_kingdomslady::getAllCheatsLv();
		}
		/******************救美最快榜********************/
		if ($type == 'best_times'){
			$object_id = isset($_GET['object_id']) ? intval($_GET['object_id']) : null;
			
			$list = App_kingdomslady::getAllAtMost('belle' , $object_id);
			$belle = App_kingdomslady::getOneBelle($object_id);
			if (empty($list) || empty($belle)){
				View::error('该信息不存在!');
			}
			
			$smarty->assign('belle' , $belle['name']);
		}
		
		$uin_arr = array();
		if (!empty($list)){
			$count = count($list);
			if ($count > 100){
				$count = 100;
			}
			$smarty->assign('count' , $count);
			foreach ($list as $k => $row){
				$uin_arr[] = $row['uin'];	
			}
			
			loadLib('User');
			$uin_arr_info = User::getBaseInfo($uin_arr);
			foreach ($list as $k => $row){
				$list_uin = $row['uin'];
				$list[$k]['name'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['name'] : '';
				$list[$k]['ranking'] = $k+1 ;
				
			}
			foreach ($list as $k => $row){
				if ($row['uin'] == $uin){
					$is_uin_count[] = $row;
				}
			}
			//print_r($is_uin_count);
			$page = ($page-1) * $per_page;
			$list = array_slice($list,$page,$per_page);
			if (!empty($is_uin_count)){
			$smarty->assign('is_uin_count' , $is_uin_count[0]);
			}
			$smarty->assign('list' , $list);
			
		}
		//print_r($list);
		$smarty->assign('per_page' , $per_page);
		
		return $smarty->fetch('app/kingdoms_lady/'.$type.'.tpl.php');
	}
	/**
	 * 美女被救最多榜
	 */
	function at_most(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$smarty = loadSmarty();
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		$list = App_kingdomslady::getAllLotOfBeauty('belle');
		$uin_arr = array();
		if (!empty($list)){
			$count = count($list);
			$smarty->assign('count' , $count);
			
			foreach ($list as $k => $row){
				$is_list_arr[] = $row['count'];
			}
			$page = ($page-1) * $per_page;
			
			/*********************count排序*********************/
			$is_list = App_kingdomslady::getArrSort($list);
			foreach ($is_list as $k => $row){
				$is_list[$k]['ranking'] = $k +1 ;
			}
			$list = array_slice($is_list,$page,$per_page);
			
			$smarty->assign('list' , $list);
		}
		//print_r($is_list);
		$smarty->assign('per_page' , $per_page);
		
		return $smarty->fetch('app/kingdoms_lady/at_most.tpl.php');
	}
	/**
	 * 太和殿初始化
	 */
	function palace(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('app/kingdoms_lady/palace.tpl.php');
	}
	/**
	 * 太和殿
	 */
	function worried(){
		$type = isset($_GET['type']) ? trim($_GET['type']) : null;
		$smarty = loadSmarty();
		if (empty($type)){
			return View::error('参数错误！');
		}
		$smarty->assign('type' , $type);
		return $smarty->fetch('app/kingdoms_lady/worried.tpl.php');
	}
	/**
	 * 皇榜初始化
	 */
	function huang_list(){
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$uin = sessionInfo('uin');
		$list = App_kingdomslady::getAllPossess($uin, 'belle');
		if (!empty($list)){
			foreach ($list as $k => $row){
				$is_k = $row['object_id'] + 1;
				if ($row['object_id'] == $is_k){
					break;
				}
			}
		}
		//print_r($is_k);
		$result = App_kingdomslady::getAllBelle();
		if (!empty($result)){
			foreach ($result as $k => $rows){
				if (!empty($is_k)){
					if ($rows['id'] == $is_k){
						$is_row = $rows;
						break;
					}
				}
			}
			
			if (empty($is_k)){
				$rs = $result[0]; 
				$smarty->assign('rs',$rs);
			}else{
				$smarty->assign('is_row',$is_row);
			}
		}
		$state = App_kingdomslady::getAllState($uin, 'map');
		
		$smarty = loadSmarty();
		if (!empty($state)){
			$time = $state[0]['end_time'] - time();
			if($time <= 0){
				App_kingdomslady::getAlterState($list[0]['object_id'], $uin, 2);
				$end_time_array =array(0,0,0);
				header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mapway&sid=' . sessionInfo('skey') );
			}else{
				$end_time_array = $time + (16 * 3600); // 因为我们时区是8
				$end_time_array = explode(':' , date('H:i:s' , $end_time_array));
			}
			$smarty->assign('time',$end_time_array);
		}
		
		//print_r($state);
		$smarty->assign('list' , $state);
		return $smarty->fetch('app/kingdoms_lady/huang_list.tpl.php');
	}
	/**
	 * 查询已救美女
	 */
	function cs_form(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$smarty = loadSmarty();
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		
		/************************查询拥有的美女********************/
		$list = App_kingdomslady::getAllPossess($uin, 'belle');
		
		if(!empty($list)){
			foreach ($list as $k => $row){
				$is_list[] = $row['object_id'];
				
			}
			$count = count($is_list);
			$smarty->assign('count' , $count);
		}
		
		/************************查询美女表***********************/
		$belle = App_kingdomslady::getAllBelle();
		
		foreach ($belle as $j => $row){
			if(!empty($list)){
				if(in_array($row['id'],$is_list)){
					$is_array[] = $row;
				}
			}
		}
		
		if(!empty($is_array)){
			foreach ($list as $k => $row){
				$is_time = App_kingdomslady::getDate($row['time']);
				foreach ($is_array as $j => $rs){
					if ($row['object_id'] == $rs['id']){
						$list[$k]['rs'] = $rs;
						
					}
				}
				$list[$k]['time'] = $is_time;
			}
			$page = ($page-1) * $per_page;
			$is_list_array = array_slice($list,$page,$per_page);
			$smarty->assign('list' , $is_list_array);
		}
		//print_r($is_list_array);
		$smarty->assign('per_page' , $per_page);
		
		return $smarty->fetch('app/kingdoms_lady/cs_form.tpl.php');
	}
	/**
	 * 任务写入跳转
	 */
	function savamixelworlds(){
		$is_k = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		if (empty($is_k)){
			$list = App_kingdomslady::getAllPossess($uin, 'belle');
			if (!empty($list)){
				foreach ($list as $k => $row){
				$is_k = $row['object_id'] + 1;
					if ($row['object_id'] == $is_k){
						break;
					}
				}
			}
		}else {
			$list = App_kingdomslady::getOnePossess($uin, $is_k);
			$is_k = $list['object_id'];
			//print_r($list);
			if (empty($list)){
				return View::error('此美女你还未解救，不能挑战！');
			}
		}
		if (empty($is_k)){
			$is_k = 1; 
		}
		$result = App_kingdomslady::getOneMixelWorlds($is_k);
		
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (!empty($data)){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}
		$map_size = $result['map_size'];
		$list = App_kingdomslady::getAllMapSize($map_size);
		
		if (empty($list)){
			return View::error('暂无可用地图');
		}
		foreach ($list as $k => $row){
			if ($row['belle_id'] == $is_k){
				$row_map = $row;
				break;
			}
		}
		
		if (empty($row_map)){
			return View::error('暂无可用地图');
		}
		
		foreach ($row_map['map'][0] as $j => $rows){
			foreach ($row_map['map'][$j] as $k => $row){
				if (!empty($row['map'])){
					if ($row['map'] == 8){
						$name[] = $j;
						$name[] = $k;
					}
					if ($row['map'] == 5){
						if (empty($row['general_id'])){
							$result = App_kingdomslady::getOneGeneralMinMax($row['ap_min'], $row['ap_max']);
							if (empty($result)){
								return View::error('武将资料不存在!!!');
							}
							$general[$j][$k] = array('id' =>$result['id'] , 'state' => 1);
						}else{
							$general[$j][$k] = array('id' =>$row['general_id'] , 'state' => 1);
						}
					}
					if (!empty($row['map_function'])){
						$function[$j][$k] = array('map_scene' => $row['map_scene'],'map_function' =>$row['map_function'] , 'function' => $row['function'] , 'state' => 1);
					}
				}
			}
		}
		if (empty($data)){
		/***********************一天不完成,任务失败**********************/
		$end_time = time() + 24 * 3600 - 1;
		/**************获取用户姓名*************/
		$doms = App_kingdomslady::getAddKingdoms($uin);
		//print_r($general);
		if (!empty($general) && !empty($function)){
			$arr_map = array('x' => $name[0] , 'y' => $name[1] , 'hp' => $doms['hp'] , 'ap' => $doms['ap'] , 'general' => $general , 'function' => $function);
		}elseif (!empty($function)){
			$arr_map = array('x' => $name[0] , 'y' => $name[1] , 'hp' => $doms['hp'] , 'ap' => $doms['ap'] , 'function' => $function);
		}elseif (!empty($general)){
			$arr_map = array('x' => $name[0] , 'y' => $name[1] , 'hp' => $doms['hp'] , 'ap' => $doms['ap'] , 'general' => $general);
		}else{
			$arr_map = array('x' => $name[0] , 'y' => $name[1] , 'hp' => $doms['hp'] , 'ap' => $doms['ap'] );
		}
		
		$code = App_kingdomslady::getAddMapState($uin, $row_map['id'], 'map', $end_time, $arr_map);
		
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
		}
	}
	/**
	 * 闯地图开始
	 */
	function mixelworlds(){
		loadLib('App_kingdomslady');
		
		$uin=sessionInfo('uin');
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($data)){
			return View::error('你还没有接受这样的任务！');
		}
		$state = $data[0]['state'];
		//print_r($data);
		/**************获取用户信息*************/
		$doms = App_kingdomslady::getAddKingdoms($uin);
		
		/*********************查询地图信息********************/
		$list = App_kingdomslady::getOneMapState($data[0]['object_id']);
		if (empty($list)){
			return View::error('没有该地图信息！');
		}
		
		//print_r($data);
		$time = $data[0]['end_time'] - time();
		//print_r($time);
		if($time <= 0){
			App_kingdomslady::getAlterState($data[0]['object_id'], $uin, 2);
		}
		$smarty = loadSmarty();
		
		$data = $data[0]['reply'];
		//print_r($data['general'][$data['x']][$data['y']]['id']);
		/*********************根据原有的坐标，查找四周的X,Y********************/
		$diection = App_kingdomslady::getDirection($data['x'], $data['y']);
		if ($list['map'][$data['x']][$data['y']]['map'] == 5){
			$id = $data['general'][$data['x']][$data['y']]['id'];
			$general = App_kingdomslady::getOneGeneral($id);
			$smarty->assign('general',$general);
		}
		/**************************获取武将资料***********************/
		$possess_list = App_kingdomslady::getAllPossessGeneral($uin, 'general');
		
		/**************************获取武功书籍***************************/
		$studio = App_kingdomslady::getDanceStudio();
		
		/*************************查找被救出的美女**********************/
		$is_belle = App_kingdomslady::getOneBelle($list['belle_id']);
		if (!empty($data['general'][$data['x']][$data['y']]['attack'])){
			$attack = $data['general'][$data['x']][$data['y']]['attack'];
			$po_map = App_kingdomslady::getAllPossessGeneralMap($uin, 'general');
			if (!empty($po_map)){
				foreach ($attack as $k => $row){
					foreach ($po_map as $l => $result){
						if (!empty($row['id'])){
							if ($row['id'] == $result['id']){
								$attack[$k]['name'] = $result;
								
							}
						}
						
					}
				}
			}
			//print_r($attack);
			$smarty->assign('attack',$attack);
		}
		loadLib('User');
		$info = User::getBaseInfo($uin);
		if (empty($info)){
			return View::error('该用户不存在!!!');
		}
		
		$smarty->assign('belle',$is_belle);
		//print_r($list);
		$smarty->assign('diection',$diection);
		$smarty->assign('data',$data);
		$smarty->assign('studio',$studio);
		$smarty->assign('possess_list',$possess_list);
		$smarty->assign('state',$state);
		$smarty->assign('info',$info);
		$smarty->assign('doms',$doms);
		$smarty->assign('list',$list);
		return $smarty->fetch('app/kingdoms_lady/map.tpl.php');
	}
	/**
	 * 进入下一个地图坐标
	 */
	function start_mixelworlds(){
		$diection = isset($_POST['diection']) ? trim($_POST['diection']) : '';
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($diection)){
			return View::error('参数错误！');
		}
		if (empty($data)){
			return View::error('你还没有接受这样的任务！');
		}
		
		$is_data = $data[0]['reply'];
		/******************读取坐标******************/
		$x_y = App_kingdomslady::getDirection($is_data['x'], $is_data['y']);
		
		if ($diection == 'up_y'){
			$x = $is_data['x'];
			$y = $x_y['up_y'];
		}elseif($diection == 'down_y'){
			$x = $is_data['x'];
			$y = $x_y['down_y'];
		}elseif($diection == 'left_x'){
			$x = $x_y['left_x'];
			$y = $is_data['y'];
		}elseif($diection == 'right_x'){
			$x = $x_y['right_x'];
			$y = $is_data['y'];
		}
		$hp = $is_data['hp'];
		
		/**********************************碰到陷阱或毒瘴**********************************/
		if ($is_data['function'][$x][$y]['map_scene'] != 2 && $is_data['function'][$x][$y]['state'] == 1 && !empty($is_data['function'][$x][$y]['state'])){
			$is_data['function'][$x][$y]['state'] = 2;
			
			$hp -= $is_data['function'][$x][$y]['function'];
			if ($hp <= 0){
				$hp = 0;
				App_kingdomslady::getAlterState($data[0]['object_id'], $uin, 3);
			}
		}
		
		if (!empty($is_data['general']) && !empty($is_data['function'])){
			$arr_map = array('x' => $x , 'y' => $y , 'hp' => $hp , 'ap' => $is_data['ap'] , 'general' => $is_data['general'] , 'function' => $is_data['function']);
		}elseif (!empty($is_data['function'])){
			$arr_map = array('x' => $x , 'y' => $y , 'hp' => $hp , 'ap' => $is_data['ap'] , 'function' => $is_data['function']);
		}elseif (!empty($is_data['general'])){
			$arr_map = array('x' => $x , 'y' => $y , 'hp' => $hp , 'ap' => $is_data['ap'] , 'general' => $is_data['general']);
		}else{
			$arr_map = array('x' => $x , 'y' => $y , 'hp' => $hp , 'ap' => $is_data['ap']);
		}
		
		/******************修改新的坐标******************/
		$code = App_kingdomslady::getAlterStateMap($data[0]['object_id'], $uin, $arr_map);
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 打开宝箱
	 */
	function treasure(){
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		/*************************查询状态信息************************/
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($data)){
			return View::error('你还没接任务！');
		}
		
		$is_data = $data[0]['reply'];
		
		$hp = $is_data['hp'];
		if ($is_data['function'][$is_data['x']][$is_data['y']]['state'] == 1){
			$is_data['function'][$is_data['x']][$is_data['y']]['state'] = 2;
			if ($is_data['function'][$is_data['x']][$is_data['y']]['map_function'] == 1){
				$hp += $is_data['function'][$is_data['x']][$is_data['y']]['function'];
				if ($hp <= 0){
					$hp = 0;
					App_kingdomslady::getAlterState($data[0]['object_id'], $uin, 3);
				}
			}elseif ($is_data['function'][$is_data['x']][$is_data['y']]['map_function'] == 2){
				loadLib('Currency');
				Currency::update('app_21' , $uin , $is_data['function'][$is_data['x']][$is_data['y']]['function']);
			}elseif ($is_data['function'][$is_data['x']][$is_data['y']]['map_function'] == 3){
			
			}
			//print_r($is_data['function'][$is_data['x']][$is_data['y']]['state']);
			if (!empty($is_data['general']) && !empty($is_data['function'])){
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap'] , 'general' => $is_data['general'] , 'function' => $is_data['function']);
			}elseif (!empty($is_data['function'])){
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap'] , 'function' => $is_data['function']);
			}elseif (!empty($is_data['general'])){
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap'] , 'general' => $is_data['general']);
			}else{
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap']);
			}
			/******************修改坐标内容******************/
			$code = App_kingdomslady::getAlterStateMap($data[0]['object_id'], $uin, $arr_map);
	
			if ($code == 0){
					header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
			}else{
					return View::error('网络繁忙,请稍后再试！');
			}
		}else{
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}
	}
	/**
	 * 挑战武将
	 */
	function attack_general(){
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		/*************************查询状态信息************************/
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($data)){
			return View::error('你还没接任务！');
		}
		/*************************查询地图信息************************/
		$list = App_kingdomslady::getOneMapState($data[0]['object_id']);
		$is_data = $data[0]['reply'];
		$id = $is_data['general'][$is_data['x']][$is_data['y']]['id'];
		//$id = $list['map'][$is_data['x']][$is_data['y']]['id'];
		/*************************查询地图武将信息************************/
		$general = App_kingdomslady::getOneGeneral($id);
		if (empty($general)){
			return View::error('武将资料不存在!!!');
		}
		/**************************获取武将资料***********************/
		$possess_list = App_kingdomslady::getAllPossessGeneral($uin, 'general');
		$is_hp = $general['hp'];
		if (!empty($possess_list)){
			foreach ($possess_list as $k => $row){
				
				$attack = App_kingdomslady::getAttackGeneral($uin, $row['ap'], $row['hp'], $general['ap'], $is_hp);
				/**************************战斗过程***********************/
				$is_data['general'][$is_data['x']][$is_data['y']]['attack'][$k] = $attack;
				$is_data['general'][$is_data['x']][$is_data['y']]['attack'][$k]['id'] = $row['id'];
				if ($attack['hp'] <= 0){
					App_kingdomslady::getAlterPossess($uin , $row['id'] , 0 , $row['ap']);
					$is_hp = $attack['is_hp'];
					$is_data['general'][$is_data['x']][$is_data['y']]['attack'][$k]['state'] = 1;
				}elseif($attack['is_hp'] <= 0){
					/****************几率获得武将*****************/
					$rand = App_kingdomslady::get_chance(30);
					$is_data['general'][$is_data['x']][$is_data['y']]['attack'][$k]['state'] = 2;
					$is_data['general'][$is_data['x']][$is_data['y']]['state'] = 0;
					break;
				}
				
			}
		}
		/**************************再次获取武将资料***********************/
		$possess_list = App_kingdomslady::getAllPossessGeneral($uin, 'general');
		
		$hp = $is_data['hp'];

		/**************************武将如果被被打败,人物继续上*************************/
		if (empty($possess_list)){
		
			$attack = App_kingdomslady::getAttackGeneral($uin, $is_data['ap'], $hp, $general['ap'], $is_hp);
			$is_data['general'][$is_data['x']][$is_data['y']]['attack'][] = $attack;
			if ($attack['hp'] <= 0){
				App_kingdomslady::getAlterState($data[0]['object_id'], $uin, 3);
				$hp = 0;
			}elseif($attack['is_hp'] <= 0){
				$hp = $attack['hp'];
				/****************几率获得武将*****************/
				$rand = App_kingdomslady::get_chance(30);
				
				$is_data['general'][$is_data['x']][$is_data['y']]['state'] = 0;
			}
		}
		if (empty($rand)){
			$rand = 0;
		}
		$is_data['general'][$is_data['x']][$is_data['y']]['handle'] = 1;

		$is_data['general'][$is_data['x']][$is_data['y']]['rand'] = $rand;
		
		if (!empty($is_data['general']) && !empty($is_data['function'])){
			$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap'] , 'general' => $is_data['general'] , 'function' => $is_data['function']);
		}elseif (!empty($is_data['function'])){
			$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap'] , 'function' => $is_data['function']);
		}elseif (!empty($is_data['general'])){
			$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $hp , 'ap' => $is_data['ap'] , 'general' => $is_data['general']);
		}

		/******************修改坐标内容******************/
		$code = App_kingdomslady::getAlterStateMap($data[0]['object_id'], $uin, $arr_map);
		//print_r($arr_map);
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 收降武将
	 */
	function collect(){
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		/*************************查询状态信息************************/
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($data)){
			return View::error('你还没接任务！');
		}
		
		$is_data = $data[0]['reply'];
		
		if (empty($is_data['general'][$is_data['x']][$is_data['y']]['rand'])){
			return View::error('没有可收服的武将!!');
		}
		
		if ($is_data['general'][$is_data['x']][$is_data['y']]['rand'] == 1){
			$id = $is_data['general'][$is_data['x']][$is_data['y']]['id'];
			$list = App_kingdomslady::getOneGeneral($id);
			if (empty($list)){
				return View::error('该武将不存在!!!');
			}
			/***********************初始化兵营************************/
			$barracks = App_kingdomslady::getAddBarracks($uin , false); 
			$newbie = false;
			if(empty($data)) {
				$barracks = App_kingdomslady::getAddBarracks($uin , true);
				$newbie = true;
			}
			/************************查询兵营等级**********************/
			$barracks_guide = App_kingdomslady::getBarracksGuide( $barracks['lv'] );
			$general = App_kingdomslady::getAllPossess($uin, 'general');
			$count = count($general);
			
			if ($count >= $barracks_guide['staff']){
				return View::error('你的兵营现达到容纳武将上限,先去升级兵营或丢弃武将!!', 0 ,array(
				array('name' => '前往兵营' , 'url' => '/app.php?m=kingdomslady&amp;a=general_initial&amp;sid=' . sessionInfo('skey')),
				));
			}
			$is_data['general'][$is_data['x']][$is_data['y']]['rand'] = 3;
			
			if (!empty($is_data['general']) && !empty($is_data['function'])){
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $is_data['hp'] , 'ap' => $is_data['ap'] , 'general' => $is_data['general'] , 'function' => $is_data['function']);
			}elseif (!empty($is_data['function'])){
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $is_data['hp'] , 'ap' => $is_data['ap'] , 'function' => $is_data['function']);
			}elseif (!empty($is_data['general'])){
				$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $is_data['hp'] , 'ap' => $is_data['ap'] , 'general' => $is_data['general']);
			}
			
			//print_r($data[0]['object_id']);
			/******************添加武将******************/
			$code = App_kingdomslady::getAddPossess($uin, $id, 'general', 0, 0);
	
			if ($code == 0){
				/******************修改坐标内容******************/
				App_kingdomslady::getAlterStateMap($data[0]['object_id'], $uin, $arr_map);
				header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
			}else{
				return View::error('网络繁忙,请稍后再试！');
			}
		}
	}
	/**
	 * 西练功房初始化
	 */
	 function west_studio_initial(){		 
		 	/********************计算时间***********************/
			$time_now=time();
			$time_exp = date("H:i:s",$time_now);
			$time_arr = explode(":",$time_exp);
			$time = (($time_arr[0]*60+$time_arr[1])*60+$time_arr[2]);//今天00：00到当前过的秒数
			$now_time = $time_now-$time;//计算当天00:00
			$next_time= $time_now+3600*24-$time;//计算明天00:00
			/**************************************************/
			$uin = sessionInfo('uin');
			$smarty = loadSmarty();
			loadLib('App_kingdomslady');
			$uin=sessionInfo('uin');
			App_kingdomslady::setDuleState($uin,$now_time,$next_time);//设置状态
			$HP=App_kingdomslady::getUserHP($uin);//获取用户生命值
			$friend_info = App_kingdomslady::getFriendInfo($uin);//获取用户的好友
			$duel_info=App_kingdomslady::getDuelNum($now_time,$next_time);//获取切磋信息
			$duel_junp=App_kingdomslady::getDuelCount($uin);//判断近卫军是否被挑战过
			$duel_now=App_kingdomslady::getDuelTime($uin);//获取切磋时间
			$duel_state=App_kingdomslady::getDuelState($uin);//获取用户与好友的状态getDuelState
			
			$T=$time_now-$duel_now['end_time'];
			//print_r($duel_junp)."123";
			if($duel_now['end_time'] > $time_now){
				$T=$duel_now['end_time']-$time_now;
				$end_time_array = $T + (16 * 3600); // 因为我们时区是8
				$end_time_array = explode(':' , date('H:i:s' , $end_time_array));
				$smarty->assign('time',$T);
				$smarty->assign('time_arr',$end_time_array);
			}else{
				$smarty->assign('time',0);
			}
			
			$smarty->assign('friend_info',$friend_info);
			$smarty->assign('HP',$HP);
			$smarty->assign('duel_state',$duel_state);
			$smarty->assign('duel_junp',$duel_junp);
			$smarty->assign('duel_info',$duel_info);
			return $smarty->fetch('app/kingdoms_lady/west_studio.tpl.php');
		}
	/**
	 *切磋界面
	 */
	function west_studio_duel(){
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$uin = sessionInfo('uin');
		$to_uin=isset($_POST['id']) ? intval($_POST['id']) : null;
		$finish_duel=isset($_POST['finish_duel']) ? intval($_POST['finish_duel']) : null;

		$now_time= time();//获取现在的时间
		$time=180;//切磋耗时
		$end_time=$now_time+$time;//切磋结束时间
 		
		$judgeRelation=App_kingdomslady::getFriendInfo($uin);//判断好友关系
		$judgeDule=App_kingdomslady::judgeDule($uin,$to_uin);
		$duel_info=App_kingdomslady::getDuelCount($uin);//今日切磋数
		$HP=App_kingdomslady::getUserHP($uin);//获取用户生命值
		$duel_now=App_kingdomslady::getDuelTime($uin);//获取用户生命值
		
		$hp=$HP['hp'];
		$duel_count = count($duel_info);
		//判断增加生命值
		if($finish_duel==1){
			App_kingdomslady::setUserHP($uin,$hp+5);//和近卫军切磋
			App_kingdomslady::setDuelState($uin,0);//修改状态
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_studio_initial&sid=' . sessionInfo('skey') );
		}elseif($finish_duel==2){
			App_kingdomslady::setUserHP($uin,$hp+10);//和好友切磋
			App_kingdomslady::setUserHP($to_uin,$hp+10);
			App_kingdomslady::setDuelState($uin,$to_uin);//修改状态
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_studio_initial&sid=' . sessionInfo('skey') );
		}
		//判断是否为好友
		if(empty($judgeRelation) && $to_uin !=0){
			return View::error('该用户不是你的好友!');
		}elseif($duel_count<2 && $duel_now['state']=='2'){
			return View::error('正在切磋中!');	
		}elseif($to_uin ==0 && $duel_count<2 && $judgeDule['state']==0) {			
			if(empty($judgeDule)){
				App_kingdomslady::addDuelInfo($now_time,$time,$end_time,$uin,0);//增加切磋信息
			}else{
				App_kingdomslady::setDuelTime($now_time,$uin,0,$end_time);//修改状态	
			}
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_studio_initial&sid=' . sessionInfo('skey') );
		}elseif($duel_count<2 && $judgeDule['state']==0){
			if(empty($judgeDule)){
				App_kingdomslady::addDuelInfo($now_time,$time,$end_time,$uin,$to_uin);//增加切磋信息
			}else{
				App_kingdomslady::setDuelTime($now_time,$uin,$to_uin);//修改状态	
			}
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_studio_initial&sid=' . sessionInfo('skey') );
		}else{
			return View::error('你今天的切磋已经达到上限!');
		}
	}
	/**
	 * 进入宣武门
	 */
	function mapway(){
		$time_now = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		$data = App_kingdomslady::getAllState($uin, 'map');
		
		if (!empty($data)){
			$is_data = $data[0];
			$list = App_kingdomslady::getOneMapState($is_data['object_id']);
			$x_y = $is_data['reply'];
			
			if (!empty($list['map'][$x_y['x']][$x_y['y']]['map'])){
				if ($list['map'][$x_y['x']][$x_y['y']]['map'] == 4){
					$id = $list['map'][$x_y['x']][$x_y['y']]['belle_id'];
					$code = App_kingdomslady::getAlterState($is_data['object_id'], $uin, 1);
					$data = App_kingdomslady::getAllState($uin, 'map');
					$is_list = App_kingdomslady::getOneBelle($id);
					$smarty->assign('list',$is_list);
					
				}
			}
			$time = $data[0]['end_time'] - $time_now;
			if($time <= 0){
				App_kingdomslady::getAlterState($is_data['object_id'], $uin, 2);
				$end_time_array =array(0,0,0);
				header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mapway&sid=' . sessionInfo('skey') );
			}else{
				$end_time_array = $time + (16 * 3600); // 因为我们时区是8
				$end_time_array = explode(':' , date('H:i:s' , $end_time_array));
			}
			$smarty->assign('time',$end_time_array);
		}
		//print_r($is_list);
		$smarty->assign('data',$data);
		return $smarty->fetch('app/kingdoms_lady/mapway.tpl.php');
	}
	/**
	 * 任务失败
	 */
	function end_state(){
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		$data = App_kingdomslady::getAllState($uin, 'map');
		//print_r($data);
		if (empty($data)){
			return View::error('你还没有接任务!');
		}
		$code = App_kingdomslady::delState($uin , $data[0]['id']);
		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mapway&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}		
	}
	/**
	 * 完成任务
	 */
	function is_mapway(){
		$smarty = loadSmarty();
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($data) || $data[0]['state'] == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mapway&sid=' . sessionInfo('skey') );
		}
		//print_r($data);
		
		if ($data[0]['state'] == 1){
			$count = App_kingdomslady::getOneCount($uin , 'belle' ,false);
			$newbie = false;
			if(empty($count)) {
				$count = App_kingdomslady::getOneCount($uin , 'belle' ,true);
				$newbie = true;
			}
			
			$is_count = $count['count'] + 1;
			$map = App_kingdomslady::getOneMapState($data[0]['object_id']);
			if (empty($map)){
				return View::error('地图错误！');
			}
			/************************被救出美女的ID***************************/
			$id = $map['map'][$data[0]['reply']['x']][$data[0]['reply']['y']]['belle_id'];
			$hp = $data[0]['reply']['hp'];
			$is_po = App_kingdomslady::getAllPossess($uin, 'belle');
			if (!empty($is_po)){
				foreach ($is_po as $k => $row){
					if ($row['object_id'] == $id){
						$result = $row;
						break;
					}
				}
			}
			
			
			if (empty($result)){
				App_kingdomslady::getAlterCount($uin, 'belle', $is_count);
				App_kingdomslady::getAddPossess($uin, $id, 'belle', 0, $hp);
			//$map['map'][]
			/**********************救美女所花费的时间************************/
			}else{
				
				 if ($result['is_ap'] > $hp){
				  	$hp = $result['is_ap'];
				  
				 }
				 //print_r($result);
				App_kingdomslady::getAlterPossess($uin, $result['id'], $result['state'], $hp);
			}	
			//print_r($result['id']);
			$code = App_kingdomslady::delState($uin , $data[0]['id']);

			if ($code == 0){
				header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mapway&sid=' . sessionInfo('skey') );
			}else{
				return View::error('网络繁忙,请稍后再试！');
			}
		}
	}
	/**
	 * 隐藏打斗记录
	 */
	function hide(){
		$state = isset($_POST['state']) ? intval($_POST['state']) : null;
		if (empty($state)){
			return View::error('参数错误！');
		}
		loadLib('App_kingdomslady');
		$uin=sessionInfo('uin');
		/*************************查询状态信息************************/
		$data = App_kingdomslady::getAllState($uin, 'map');
		if (empty($data)){
			return View::error('你还没接任务！');
		}
		$is_data = $data[0]['reply'];
		$is_data['general'][$is_data['x']][$is_data['y']]['handle'] = $state;
		
		if (!empty($is_data['general']) && !empty($is_data['function'])){
			$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $is_data['hp'] , 'ap' => $is_data['ap'] , 'general' => $is_data['general'] , 'function' => $is_data['function'] , 'attack' => $is_data['attack']);
		}elseif (!empty($is_data['function'])){
			$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $is_data['hp'] , 'ap' => $is_data['ap'] , 'function' => $is_data['function']);
		}elseif (!empty($is_data['general'])){
			$arr_map = array('x' => $is_data['x'] , 'y' => $is_data['y'], 'hp' => $is_data['hp'] , 'ap' => $is_data['ap'] , 'general' => $is_data['general'] , 'attack' => $is_data['attack']);
		}
		
		/******************修改坐标内容******************/
		$code = App_kingdomslady::getAlterStateMap($data[0]['object_id'], $uin, $arr_map);

		if ($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 藏宝楼初始化
	 */
	function oneselfprop_initial(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$smarty = loadSmarty();
		$uin=sessionInfo('uin');
		/*******************查询道具******************/
		loadLib('App_prop');
		$list = App_prop::getAllOneselfProp('app_kingdoms_oneself_prop','app_kingdoms_prop',$uin);
		$count = count($list);
		/*******************数据分页******************/
		$page = ($page-1) * $per_page;
		
		$is_list_array = array_slice($list,$page,$per_page);
		loadLib('App_kingdomslady');
		/**********************查询是否闯关!!!*************************/
		$data = App_kingdomslady::getAllState($uin, 'map');
		
		$is_list = App_kingdomslady::getDuelState($uin);
		/************************获取时间********************/
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		
		if (!empty($is_list)){
			if ($is_list['end_time'] - $time < 0){
				$smarty->assign('is_list',$is_list);
			}
		}
		
		if (!empty($data)){
			if ($data[0]['end_time'] - $time > 0){
				$smarty->assign('data',$data);
			}
		}
		
		$smarty->assign('list',$is_list_array);
		$smarty->assign('per_page',$per_page);
		$smarty->assign('count',$count);
		return $smarty->fetch('app/kingdoms_lady/oneselfprop.tpl.php');
	}
	/**
	 * 使用道具
	 */
	function use_prop(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_kingdoms_prop', $id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		$uin=sessionInfo('uin');
		/**********************查询自己的道具************************/
		
		$prop = App_prop::getOneselfProp('app_kingdoms_oneself_prop', $uin, $id);
		if (empty($prop)){
			return View::error('你没有此道具!!!' , 0 ,array(
				array('name' => '前往商店' , 'url' => '/app.php?m=kingdomslady&amp;a=prop_initial&amp;sid=' . sessionInfo('skey')),
				));
		}
		loadLib('App_kingdomslady');
		if ($list['type'] == 'hp' || $list['type'] == 'ap'){
			/**********************查询是否闯关!!!*************************/
			$data = App_kingdomslady::getAllState($uin, 'map');
			if (empty($data) || $data[0]['state'] !=0){
				return View::error('你还未接任务或者已经完成!!!',0, array(
					array('name' => '前往皇榜' , 'url' => '/app.php?m=kingdomslady&amp;a=huang_list&amp;sid=' . sessionInfo('skey')),
				));
			}
			$data[0]['reply'][$list['type']] += $list['harm'];
			$code = App_kingdomslady::getAlterStateMap($data[0]['object_id'], $uin, $data[0]['reply']);
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=mixelworlds&sid=' . sessionInfo('skey') );
		}
		if ($list['type'] == 'diligent') {
			$count = App_kingdomslady::getOneCount($uin , 'dance_studio' , false , 2);
			$newbie = false;
			if(empty($count)) {
				$count = App_kingdomslady::getOneCount($uin , 'dance_studio' , true ,2);
				$newbie = true;
			}
			if ($count['count'] >= 3){
				return View::error('该物品今天你已使用一次!!!');
			}
			$count['count'] += 1;
			$code = App_kingdomslady::getAlterCount($uin, 'dance_studio', $count['count']);
			header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=dance_studio_initial&sid=' . sessionInfo('skey') );
		}
		if ($list['type'] == 'steal') {
			$is_list = App_kingdomslady::getDuelState($uin);
			/************************获取时间********************/
			$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			
			if (!empty($is_list)){
				if ($is_list['end_time'] - $time < 0){
					$is_to_prop = App_prop::getAllOneselfProp('app_kingdoms_oneself_prop','app_kingdoms_prop',$is_list['to_uin']);
					
					if (!empty($is_to_prop)){
						foreach ($is_to_prop as $k => $row){
							if ($row['type'] == 'guard'){
								$is_row = $row;
							}
						}
						
						if (!empty($is_row)){
							$is_count = $is_row['count'] -1;
							
							if ($is_count <= 0){
								App_prop::delProp($is_list['to_uin'], $is_row['id']);
							}else{
								App_prop::getAlterOneselfProp('app_kingdoms_oneself_prop',$is_row['id'], $is_list['to_uin'], $is_count, $is_row['time']);
							}
							header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_prop&state=8&sid=' . sessionInfo('skey') );
						}else{
							$rand_count = count($is_to_prop) - 1;
							$rand = rand(0, $rand_count); 
							//$uin_rand = rand(1, $is_to_prop[$rand]['count']);
							$to_count = intval($is_to_prop[$rand]['count'] / 5);
							if ($to_count == 0){
								$to_count = 1;
							}else{
								$to_count = rand(1, $to_count);
							}
							print_r($to_count);
							$is_count = $is_to_prop[$rand]['count'] - $to_count;
							
							if ($is_count <= 0){
								App_prop::delProp('app_kingdoms_oneself_prop',$is_list['to_uin'], $is_to_prop[$rand]['id']);
							}else{
								App_prop::getAlterOneselfProp('app_kingdoms_oneself_prop',$is_to_prop[$rand]['id'], $is_list['to_uin'], $is_count, $is_to_prop[$rand]['time']);
							}
							/**********************查询自己的道具************************/
							$uin_prop = App_prop::getOneselfProp('app_kingdoms_oneself_prop', $uin, $is_to_prop[$rand]['id']);
							/**********************记录为空时写入************************/
							if (empty($uin_prop)){
								App_prop::getAddOneselfProp('app_kingdoms_oneself_prop', $uin, $is_to_prop[$rand]['id'], 1);
							}else{
								
								$count = $uin_prop['count'] + $to_count;
								App_prop::getAlterOneselfProp('app_kingdoms_oneself_prop', $is_to_prop[$rand]['id'], $uin, $count, $uin_prop['time']);
							}
							
							header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_prop&state=9&count='.$to_count.'&to_uin='.$is_list['to_uin'].'&id='.$is_to_prop[$rand]['id'].'&sid=' . sessionInfo('skey') );
						}
						
					}else{
						header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=west_prop&state=7&sid=' . sessionInfo('skey') );
					}
					App_kingdomslady::setDuelState($uin,$is_list['to_uin']);//修改状态
				}else{
					return View::error('你的切磋还未完成!!!',0, array(
					array('name' => '前往切磋房' , 'url' => '/app.php?m=kingdomslady&amp;a=west_studio_initial&amp;sid=' . sessionInfo('skey')),
				));
				}
			}else{
				return View::error('你还没切磋或者切磋已达到上限!!!',0, array(
					array('name' => '前往切磋房' , 'url' => '/app.php?m=kingdomslady&amp;a=west_studio_initial&amp;sid=' . sessionInfo('skey')),
				));
			}
		}
		if ($code == 0){
			$count = $prop['count'] -1;
			if ($count <= 0){
				App_prop::delProp('app_kingdoms_oneself_prop',$uin, $id);
			}else{
				App_prop::getAlterOneselfProp('app_kingdoms_oneself_prop',$id, $uin, $count, $prop['time']);
			}
		}else{
				return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 切磋完成使用道具
	 */
	function west_prop(){
		$state = isset($_GET['state']) ? intval($_GET['state']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$to_uin = isset($_GET['to_uin']) ? intval($_GET['to_uin']) : null;
		$count = isset($_GET['count']) ? intval($_GET['count']) : null;
		if (empty($state)){
			return View::error('参数错误!!!');
		}
		$smarty = loadSmarty();
		if (!empty($id)){
			if (empty($to_uin) || empty($count)){
				return View::error('参数错误!!!!');
			}
			loadLib('App_prop');
			$uin=sessionInfo('uin');
			$prop = App_prop::getAllOneselfProp('app_kingdoms_oneself_prop', 'app_kingdoms_prop', $to_uin);
			//$prop = App_prop::getOneselfProp('app_kingdoms_oneself_prop', $uin, $id);
			if (!empty($prop)){
				foreach ($prop as $k => $row){
					if ($row['id'] == $id){
						$list = $row;
						break;
					}
				}
				if (empty($list)){
					return View::error('该道具不存在!!!');
				}else{
					$smarty->assign('list',$list);
				}
			}
			
		}
		$smarty->assign('count',$count);
		$smarty->assign('state',$state);
		
		return $smarty->fetch('app/kingdoms_lady/west_duel_prop.tpl.php');
	}
	/**
	 * 商店初始化
	 */
	function prop_initial(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$smarty = loadSmarty();
		$uin=sessionInfo('uin');
		/*******************查询道具******************/
		loadLib('App_prop');
		$list = App_prop::getAllProp('app_kingdoms_prop');
		$count = count($list);
		
		/*******************数据分页******************/
		$page = ($page-1) * $per_page;
		$is_list_array = array_slice($list,$page,$per_page);
		
		$smarty->assign('list',$is_list_array);
		$smarty->assign('per_page',$per_page);
		$smarty->assign('count',$count);
		
		return $smarty->fetch('app/kingdoms_lady/prop.tpl.php');
	}
	/**
	 * 购买道具初始化
	 */
	function prop(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_kingdoms_prop', $id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		$uin=sessionInfo('uin');
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('app_kingdoms_oneself_prop', $uin, $id);
		
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		
		$smarty = loadSmarty();
		$smarty->assign('list',$list);
		$smarty->assign('prop',$prop);
		$smarty->assign('money',$money);
		return $smarty->fetch('app/kingdoms_lady/buy_prop.tpl.php');
	}
	/**
	 * 购买道具
	 */
	function buy_prop(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$photoscope = isset($_POST['photoscope']) ? intval($_POST['photoscope']) : null;
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_kingdoms_prop', $id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		$uin=sessionInfo('uin');
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		$gold = $photoscope * $list['price'];
		if ($gold > $money['gold']){
			return View::error('你的金币不足'.$gold.',请及时充值!!!');
		}
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('app_kingdoms_oneself_prop', $uin, $id);
		/**********************记录为空时写入************************/
		if (empty($prop)){
			App_prop::getAddOneselfProp('app_kingdoms_oneself_prop', $uin, $id, $photoscope);
		}else{
			
			$count = $prop['count'] + $photoscope;
			App_prop::getAlterOneselfProp('app_kingdoms_oneself_prop', $id, $uin, $count, $prop['time']);
		}
		/***********************扣除金币********************/
		Money::update($uin, -$gold);
		loadLib('Operate');
		Operate::getAddConsume($uin, 'app_kingdoms_prop', $id, $gold);
		header('Location: ' . SITEURL . '/app.php?m=kingdomslady&a=prop_initial&sid=' . sessionInfo('skey') );
	}
	/**
	 * build
	 */
	function build(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('app/kingdoms_lady/build.tpl.php');
	}
	
}
# end of script